﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RobotView.Properties;
using RobotCtrl;

namespace RobotView
{
    public partial class SwitchView : UserControl
    {
        private bool state;
        private Switch swi;

        public SwitchView()
        {
            InitializeComponent();
        }

        public bool State
        {
            get { return state; }
            set
            {
                state = value;
                if (state)
                {
                    pictureBox1.Image = Resource.SwitchOn;
                }
                else
                {
                    pictureBox1.Image = Resource.SwitchOff;
                }
            }
        }

        public Switch Switch
        {
            get { return swi; }
            set
            {
                if (null != swi) { swi.SwitchStateChanged -= swi_SwitchStateChanged; }
                swi = value;
                if (null != swi) { swi.SwitchStateChanged += swi_SwitchStateChanged; }
            }
        }

        /// <summary>
        /// Methode wird aufgerufen, wenn der Zustand der Schalter ändert.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>      
        void swi_SwitchStateChanged(object sender, SwitchEventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler<SwitchEventArgs>(swi_SwitchStateChanged), sender, e);
            }
            else
            State = e.SwitchEnabled;
        }
    }
}
